<template>
	<view class="container">
		<!-- <view v-if="count > 1" class="acea-row row-around nav"> -->
		<view v-if="false" class="acea-row row-around nav">
			<template v-for="item in navList">
				<view v-if="item.count" :key="item.type" :class="['acea-row', 'row-middle', type === item.type ? 'on' : '']" @click="setType(item.type)">{{ item.name }}</view>
			</template>
		</view>
		<!-- <view v-if="count > 1" style="height: 106rpx;"></view> -->
    <view class="new-coupon-list"  v-if="couponsList.length">
        <view class="item"  v-for="(item,index) in couponsList" :key="index">
            <view class="hd">
              <view class="price"><text class="unit">￥</text>{{item.coupon_price}}</view>
              <view class="desc">
                <view class="text1">
                  {{item.title}}
                </view>
                <view class="text2"><view v-if="item.coupon_time">{{$t(`领取后`)}} {{item.coupon_time}} {{$t(`天内可用`)}}</view>
                  <view v-else>{{ item.start_use_time ? item.start_use_time + '-' : '' }}{{ item.end_use_time }}</view></view>
              </view>
            </view>
            <view class="bd">
              <view class="rule-box">
                <view class="rule" @tap="handleRule(item, index)">使用规则<image :src="item.showRule ? '../static/arrow_up.png' : '../static/arrow_down.png'" class="arrow-img" /></view>
                <view class="btn gray" v-if="item.is_use >= item.receive_limit">{{$t(`已领取`)}}</view>
                <view class="btn gray" v-else-if="item.is_permanent == 0 && item.remain_count == 0">{{$t(`已领完`)}}</view>
                <view class="btn" v-else @click="getCoupon(item.id, index)">{{$t(`领取`)}}</view>
              </view>
              <view class="rule-text" v-if="item.showRule">
                1、自绑定日起180天内使用有效，租期包含任意法定节假日(含公休及调休)及前一天不能使用;
                2、单笔订单租期满2天及以上可用，适用于经济或舒适车型，仅限非首次取车订单可用，仅减免最低一日【车辆租赁费及门店服务费】，最高可减200元，其他费用正常收取;
                3、不可与一嗨其他优惠共享，不可重复使用，仅限一嗨国内自驾业务使用;
                4、该券仅限账户本人使用，不可转让，实际承租人非本人订单不可用。
              </view>
            </view>
        </view>
    </view>
    
	<!-- 	<view class='coupon-list' v-if="couponsList.length">
			<view class='item acea-row row-center-wrapper' v-for="(item,index) in couponsList" :key="index" :class="{svip: item.receive_type === 4}">
				<view class="moneyCon acea-row row-center-wrapper">
					<view class='money' :class='item.is_use >= item.receive_limit ? "moneyGray" : "" '>
						<view>{{$t(`￥`)}}<text class='num'>{{item.coupon_price}}</text></view>
						<view class="pic-num" v-if="item.use_min_price > 0">{{$t(`满`)}} {{item.use_min_price}} {{$t(`元可用`)}}</view>
						<view class="pic-num" v-else>{{$t(`无门槛券`)}}</view>
					</view>
				</view>
				<view class="text">
					<view class="condition">
						<view class="name line2">
							<view class="line-title" :class="item.is_use >= item.receive_limit ? 'bg-color-huic' : ''" v-if="item.type === 0">{{$t(`通用劵`)}}</view>
							<view class="line-title" :class="item.is_use >= item.receive_limit ? 'bg-color-huic' : ''" v-else-if="item.type === 1">{{$t(`品类券`)}}</view>
							<view class="line-title" :class="item.is_use >= item.receive_limit ? 'bg-color-huic' : ''" v-else>{{$t(`商品券`)}}</view>
							<image v-if="item.receive_type === 4" class="pic" src="https://taxi.raxan.xyz/wechat/static/images/fvip.png"></image>{{ $t(item.title) }}
						</view>
					</view>
					<view class="data acea-row row-between-wrapper">
						<view v-if="item.coupon_time">{{$t(`领取后`)}} {{item.coupon_time}} {{$t(`天内可用`)}}</view>
						<view v-else>{{ item.start_use_time ? item.start_use_time + '-' : '' }}{{ item.end_use_time }}</view>
						<view class="bnt gray" v-if="item.is_use >= item.receive_limit">{{$t(`已领取`)}}</view>
						<view class="bnt gray" v-else-if="item.is_permanent == 0 && item.remain_count == 0">{{$t(`已领完`)}}</view>
						<view class="bnt theme-color-bg" v-else @click="getCoupon(item.id, index)">{{$t(`立即领取`)}}</view>
					</view>
				</view>
			</view>
		</view> -->
    
		<view class='loadingicon acea-row row-center-wrapper' v-if="couponsList.length">
			<text class='loading iconfont icon-jiazai' :hidden='loading==false'></text>{{loadTitle}}
		</view>
		<view class='noCommodity' v-else-if="!couponsList.length && page === 2">
			<view class='pictrue'>
				<!-- <image :src="imgHost + '/statics/images/noCoupon.png'"></image> -->
        <image src="../static/no_coupon.png"></image>
        <view class="no-data-txt">暂无优惠券可使用哦~</view>
			</view>
		</view>
    <view class="operate-box" @tap="jumpCouponRecord">
        <view class="btn">
          优惠券历史记录
        </view>
    </view>
		<!-- #ifdef MP -->
		<authorize @onLoadFun="onLoadFun" @authColse="authColse"></authorize>
		<!-- #endif -->
		<!-- #ifndef MP -->
		<!-- <home></home> -->
		<!-- #endif -->
	</view>
</template>

<script>
	import { getCoupons, setCouponReceive } from '@/api/api.js';
	import { toLogin } from '@/libs/login.js';
	import { mapGetters } from "vuex";
	// #ifdef MP
	import authorize from '@/components/Authorize';
	// #endif
	import home from '@/components/home';
	import colors from '@/mixins/color.js';
	import {HTTP_REQUEST_URL} from '@/config/app';
	export default {
		components: {
			// #ifdef MP
			authorize,
			// #endif
			home
		},
		mixins:[colors],
		data() {
			return {
				imgHost:HTTP_REQUEST_URL,
				couponsList: [],
				loading: false,
				loadend: false,
				loadTitle: this.$t(`加载更多`), //提示语
				page: 1,
				limit: 20,
				isAuto: false, //没有授权的不会自动授权
				isShowAuth: false, //是否隐藏授权
				type: 0,
				navList: [{
						type: 0,
						name: this.$t(`通用券`),
						count: 0
					},
					{
						type: 1,
						name: this.$t(`品类券`),
						count: 0
					},
					{
						type: 2,
						name: this.$t(`商品券`),
						count: 0
					},
				],
				count: 0,
			};
		},
		computed: mapGetters(['isLogin']),
		watch: {
			isLogin: {
				handler: function(newV, oldV) {
					if (newV) {
						this.getUseCoupons();
					}
				},
				deep: true
			}
		},
		onLoad() {
			if (this.isLogin) {
				// #ifdef H5 || APP-PLUS
				this.getUseCoupons();
				// #endif
			} else {
				toLogin();
			}
		},
		/**
		 * 页面上拉触底事件的处理函数
		 */
		onReachBottom: function() {
			this.getUseCoupons();
		},
		methods: {
			onLoadFun() {
				this.getUseCoupons();
			},
			// 授权关闭
			authColse: function(e) {
				this.isShowAuth = e;
			},
      jumpCouponRecord(){
        uni.navigateTo({
          url: `/pages/users/user_coupon/record`
        })
      },
      handleRule(item, index){
          let {couponsList} = this
        	this.couponsList = couponsList.map((item, idx) => {
            if(idx == index){
              item.showRule = !item.showRule
            }
            return item;
          })
      },
			getCoupon: function(id, index) {
				let that = this;
				let list = that.couponsList;
				//领取优惠券
				setCouponReceive(id).then(function(res) {
					list[index].is_use = list[index].is_use + 1;
					that.$set(that, 'couponsList', list);
					that.$util.Tips({title: that.$t(`领取成功`)});
				}).catch(error => {
					return that.$util.Tips({title: error});
				})
			},
			/**
			 * 获取领取优惠券列表
			 */
			getUseCoupons: function() {
				let that = this
				if (this.loadend) return false;
				if (this.loading) return false;
				that.loading = true;
				that.loadTitle = that.$t(`加载更多`);
				getCoupons({
					type: that.type,
					page: that.page,
					limit: that.limit
				}).then(res => {
					let list = res.data.list,
						loadend = list.length < that.limit;
					let couponsList = that.$util.SplitArray(list, that.couponsList);
          console.log('couponsList', couponsList)
					res.data.count.forEach((value, index) => {
						that.navList[index].count = value;
						if (value) {
							that.count++;
						}
					});
					that.$set(that, 'couponsList', couponsList);
					that.loadend = loadend;
					that.loading = false;
					that.loadTitle = loadend ? that.$t(`我也是有底线的`) : that.$t(`加载更多`);
					that.page = that.page + 1;
				}).catch(err => {
					that.loading = false;
					that.loadTitle = that.$t(`加载更多`);
				});
			},
			setType: function(type) {
				if (this.type !== type) {
					this.type = type;
					this.couponsList = [];
					this.page = 1;
					this.loadend = false;
					this.getUseCoupons();
				}
			}
		}
	};
</script>

<style scoped lang="scss">
  .container{
    padding-bottom: 180rpx;
  }
	.nav {
		position: fixed;
		top: 0;
		left: 0;
		width: 100%;
		height: 106rpx;
		background-color: #FFFFFF;
		font-size: 30rpx;
		color: #999999;
		z-index: 9;
	}

	.nav .acea-row {
		border-top: 5rpx solid transparent;
		border-bottom: 5rpx solid transparent;
		cursor: pointer;
	}

	.nav .acea-row.on {
		border-bottom-color: var(--theme-color-left);
		color: #282828;
	}
	
	.coupon-list .pic-num {
		color: #FFFFFF;
		font-size: 24rpx;
	}

	.coupon-list .item .text .condition {
		display: flex;
		align-items: center;
	}
	
	.coupon-list .item .text .condition .name {
		font-size: 26rpx;
		font-weight: 500;
		line-height: 40rpx;
		/* display: flex;
		align-items: center; */
	}
	
	.coupon-list .item .text .condition .pic {
		width: 30rpx;
		height: 30rpx;
		display: block;
		margin-right: 10rpx;
		display: inline-block;
		vertical-align: middle;
	}
  
  .new-coupon-list{
    padding: 24rpx 32rpx;
    .item{
      margin-bottom: 24rpx;
      padding: 32rpx 30rpx 0 30rpx;
      background: #FFF;
      border-radius: 16rpx;
      .hd{
        display: flex;
        align-items: center;
        border-bottom: 1rpx solid #E7E7E7;
        padding-bottom: 23rpx;
        .price{
          margin-right: 40rpx;
          font-weight: 500;
          font-size: 50rpx;
          color: #FF5700;
          .unit{
            font-size: 30rpx;
            color: #FF5700;
          }
        }
        .desc{
          .text1{
            margin-bottom: 8rpx;
            font-weight: 600;
            font-size: 32rpx;
            color: #000000;
          }
          .text2{
            display: flex;
            font-weight: 400;
            font-size: 24rpx;
            color: #777B82;
          }
        }
      }
      .bd{
        .rule-box{
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 24rpx 0;
            .rule{
              display: flex;
              align-items: center;
              font-size: 24rpx;
              color: rgba(0,0,0,0.6);
              image{
                width: 40rpx;
                height: 40rpx;
                margin-left: 5rpx;
              }
            }
            .btn{
              padding: 12rpx 30rpx;
              background: #FF7200;
              border-radius: 200rpx 200rpx 200rpx 200rpx;
              font-size: 26rpx;
              color: #FFFFFF;
              &.gray{
                 background: #ccc;
              }
            }
        }
        .rule-text{
          padding-bottom: 20rpx;
          font-weight: 400;
          font-size: 24rpx;
          color: rgba(0,0,0,0.6);
          line-height: 40rpx;
        }
      }
    }
  }
  
  .operate-box{
    width: 100%;
    position: fixed;
    left: 0;
    bottom: 0;
    background: #FFF;
    padding: 20rpx 0;
    .btn{
     display: block;
     margin: 0 auto;
     width: 400rpx;
     height: 96rpx;
     line-height: 96rpx;
     text-align: center;
     border-radius: 12rpx 12rpx 12rpx 12rpx;
     border: 2rpx solid #C5C5C5;
     font-size: 32rpx;
     color: rgba(0,0,0,0.9);
    }
  }
	
	.condition .line-title {
		width: 90rpx;
		height: 40rpx !important;
		line-height: 40rpx;
		text-align: center;
		box-sizing: border-box;
		background: rgba(225, 247, 255, 1.0);
		border: 1rpx solid var(--theme-color-left);
		opacity: 1;
		border-radius: 20rpx;
		font-size: 18rpx !important;
		color: var(--theme-color-left);
		margin-right: 12rpx;
		text-align: center;
		display: inline-block;
		vertical-align: middle;
	}
	
	.condition .line-title.bg-color-huic {
		border-color: #BBB!important;
		color: #bbb!important;
		background-color: #F5F5F5!important;
	}
  .noCommodity {
  	margin-top: 200rpx;
    .no-data-txt{
      margin-top: 10rpx;
      font-size: 26rpx;
      color: #B9B9BD;
      text-align: center;
    }
  }
</style>
